// ------------------------------------
// Reducer
// ------------------------------------
import { createReducer, statusToError, getStatusError } from 'http-services'
import _ from 'lodash'
import * as types from './constant'

const initState = {
  getListPending: false,
  getListError: -1,
  getListMessage: null,
  listData: null,
  options: null,
  createPending: false,
  createError: -1,
  createMessage: null,
  editPending: false,
  editError: -1,
  editMessage: null,
  removePending: false,
  removeError: -1,
  removeMessage: null,
  statusPending: false,
  statusError: -1,
  statusMessage: null
}

const ACTION_HANDLERS = {
  [types.ADMINSUSER_FETCH_LIST_BEGIN]: (state, action) => {
    return {
      ...state,
      getListPending: true,
      getListError: -1,
      getListMessage: null
    }
  },
  [types.ADMINSUSER_FETCH_LIST_SUCCESS]: (state, action) => {
    const { payload } = action
    const { data, Status } = payload
    let newState = null
    if (data && Status.code === 0) {
      newState = { listData: data.list, options: data.options }
    }
    return {
      ...state,
      ...statusToError(payload, 'getListError', 'getListMessage'),
      getListPending: false,
      ...newState
    }
  },
  [types.ADMINSUSER_FETCH_LIST_FAILURE]: (state, action) => {
    const { error } = action
    const status = getStatusError(error)
    return {
      ...state,
      ...statusToError({ status }, 'getListError', 'getListMessage'),
      getListPending: false,
    }
  },
  [types.ADMINSUSER_FETCH_CREATE_BEGIN]: (state, action) => {
    return {
      ...state,
      createPending: true,
      createError: -1,
      createMessage: null
    }
  },
  [types.ADMINSUSER_FETCH_CREATE_SUCCESS]: (state, action) => {
    const { payload } = action
    const { data, Status } = payload
    let newState = null
    if (data && Status.code === 0) {
      newState = { listData: data.list, options: data.options }
    }
    return {
      ...state,
      ...statusToError(payload, 'createError', 'createMessage'),
      createPending: false,
      ...newState
    }
  },
  [types.ADMINSUSER_FETCH_CREATE_FAILURE]: (state, action) => {
    const { error } = action
    const status = getStatusError(error)
    return {
      ...state,
      ...statusToError({ status }, 'createError', 'createMessage'),
      createPending: false,
    }
  },
  [types.ADMINSUSER_FETCH_EDIT_BEGIN]: (state, action) => {
    return {
      ...state,
      editPending: true,
      editError: -1,
      editMessage: null
    }
  },
  [types.ADMINSUSER_FETCH_EDIT_SUCCESS]: (state, action) => {
    const { payload } = action
    const { data, Status } = payload
    let newState = null
    if (data && Status.code === 0) {
      let listData = state.listData
      let useIndex = _.findIndex(listData, { _id: data._id })
      listData[useIndex] = data
      newState = { listData }
    }
    return {
      ...state,
      ...statusToError(payload, 'editError', 'editMessage'),
      editPending: false,
      ...newState
    }
  },
  [types.ADMINSUSER_FETCH_EDIT_FAILURE]: (state, action) => {
    const { error } = action
    const status = getStatusError(error)
    return {
      ...state,
      ...statusToError({ status }, 'editError', 'editMessage'),
      editPending: false,
    }
  },
  [types.ADMINSUSER_FETCH_REMOVE_BEGIN]: (state, action) => {
    return {
      ...state,
      removePending: true,
      removeError: -1,
      removeMessage: null
    }
  },
  [types.ADMINSUSER_FETCH_REMOVE_SUCCESS]: (state, action) => {
    const { payload } = action
    const { data, Status } = payload
    let newState = null
    if (data && Status.code === 0) {
      newState = { listData: data.list, options: data.options }
    }
    return {
      ...state,
      ...statusToError(payload, 'removeError', 'removeMessage'),
      removePending: false,
      ...newState
    }
  },
  [types.ADMINSUSER_FETCH_REMOVE_FAILURE]: (state, action) => {
    const { error } = action
    const status = getStatusError(error)
    return {
      ...state,
      ...statusToError({ status }, 'removeError', 'removeMessage'),
      removePending: false,
    }
  },
  [types.ADMINSUSER_FETCH_STATUS_BEGIN]: (state, action) => {
    return {
      ...state,
      statusPending: true,
      statusError: -1,
      statusMessage: null
    }
  },
  [types.ADMINSUSER_FETCH_STATUS_SUCCESS]: (state, action) => {
    const { payload } = action
    const { data, Status } = payload
    let newState = null
    if (data && Status.code === 0) {
      newState = { listData: data.list, options: data.options }
    }
    return {
      ...state,
      ...statusToError(payload, 'statusError', 'statusMessage'),
      statusPending: false,
      ...newState
    }
  },
  [types.ADMINSUSER_FETCH_STATUS_FAILURE]: (state, action) => {
    const { error } = action
    const status = getStatusError(error)
    return {
      ...state,
      ...statusToError({ status }, 'statusError', 'statusMessage'),
      statusPending: false,
    }
  },
}

export default (state = initState, action) => createReducer(state, action, ACTION_HANDLERS)